#region Summary

// 
// Owner : JackieHan 
// 
// Solution : DailyCollection 
// 
// Project : Jackie.Utility.Notify
// 
// File : SubscribeService.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : 订阅接口的服务实现
// 
// History : 
//			2011-04-26 15:56:12	    Create 
// 

#endregion

#region Using

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using Common.Logging;
using Jackie.Utility.Notify.Domain;

#endregion

namespace Jackie.Utility.Notify.Service
{
    /// <summary> 
    /// 订阅接口的服务实现
    /// </summary>
    public class SubscribeService : ISubscribe
    {
        private ILog logger;

        public SubscribeService()
        {
            logger = LogManager.GetLogger(GetType());
        }

        public void Subscribe(string clientSign, string title)
        {
            logger.Info(string.Format("start subsribe message!title:{0},client:{1}", title, clientSign));
            var callback = OperationContext.Current.GetCallbackChannel<IReceivedMessage>();
            var titleInfo = TitleCollection.GetInstance().GetTitle(title);
            var client = new ClientSubscribeInfo() { CallbackInstance = callback, ClientSign = clientSign, Title = titleInfo };
            ClientSubscribeCollection.GetInstance().Add(client);
            logger.Info("end subsribe message!");
        }

        public void Unsubscribe(string clientSign, string title)
        {
            logger.Info(string.Format("start unsubsribe message!title:{0},client:{1}", title, clientSign));
            ClientSubscribeCollection.GetInstance().Remove(clientSign, title);
            logger.Info("end unsubsribe message!");
        }

        public IEnumerable<TitleInfo> GetAllTitles()
        {
            return TitleCollection.GetInstance().GetAllTitles().Values;
        }
    }
}